import isEmpty from 'lodash-es/isEmpty';

const dateStringSortFunction = (a: string, b: string) => {
  if (isEmpty(a) && isEmpty(b)) {
    return 0;
  }
  if (isEmpty(a) && !isEmpty(b)) {
    return -1;
  }
  if (isEmpty(b) && !isEmpty(a)) {
    return 1;
  }
  const aDate = new Date(a);
  const bDate = new Date(b);
  return aDate.getTime() - bDate.getTime();
};

const formatDuration = (minutes: number) => {
  if (!minutes) {
    return '/';
  }
  const hour = Math.floor(minutes / 60);
  const minute = (minutes % 60).toFixed(0);
  return hour == 0 ? `${minute} min` : `${hour} h ${minute} min`;
};

export { dateStringSortFunction, formatDuration };
